package com.wwwarehouse.common.adapter.filter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import com.wwwarehouse.common.R;
import com.wwwarehouse.common.bean.filter.FilterBean;
import com.wwwarehouse.common.tools.rsa.StringUtils;

import java.util.List;

/**
 *  多选筛选器的Adapter
 * Created by ShineF on 2017/7/11 0011.
 */

public class FilterMultipleAdapter extends BaseAdapter {
    private Context mContext;
    private List<FilterBean> mList;
    private boolean mIsSingle;
    private ViewHolder mHolder;
    private OnDrawerItemClick onDrawerItemClick;
    private String title;

    public FilterMultipleAdapter(Context context, List<FilterBean> list) {
        this.mContext = context;
        this.mList = list;

    }

    public FilterMultipleAdapter(Context context, List<FilterBean> list, boolean isSingle, String title) {
        this.mContext = context;
        this.mList = list;
        this.mIsSingle = isSingle;
        this.title = title;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if(convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_drawer_filter_multiple, null);
            mHolder = new ViewHolder(convertView);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }

        final FilterBean filterBean = mList.get(position);

        if (filterBean.isSelect()) {
            mHolder.mBtn.setSelected(true);
        } else {
            mHolder.mBtn.setSelected(false);
        }

        mHolder.mBtn.setText(filterBean.getText());
        mHolder.mBtn.setTag(StringUtils.isNullString(title) ? "" : title);

        if (mIsSingle) {
            mHolder.mBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (FilterBean filterBean : mList) {
                        filterBean.setSelect(false);
                    }
                    String title = (String) v.getTag();
                    if(onDrawerItemClick != null){
                        onDrawerItemClick.onClick(position, v, !filterBean.isSelect(), title);
                    }
                    filterBean.setSelect(!filterBean.isSelect());

                    notifyDataSetChanged();
                }
            });
        } else {
            mHolder.mBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String title = (String) v.getTag();
                    if(onDrawerItemClick != null){
                        onDrawerItemClick.onClick(position, v, !filterBean.isSelect(), title);
                    }
                    filterBean.setSelect(!filterBean.isSelect());
                    notifyDataSetChanged();
                }
            });
        }

        return convertView;
    }

    public void setOnDrawerItemClick(OnDrawerItemClick onDrawerItemClick){
        this.onDrawerItemClick = onDrawerItemClick;
    }

    public interface OnDrawerItemClick{
        void onClick(int pos, View view, boolean isSelected, String title);
    }

    class ViewHolder{
        Button mBtn;

        public ViewHolder(View view){
                mBtn = (Button) view.findViewById(R.id.btn_item);
        }
    }
}
